package com.baike.controller;

import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbOrder;
import com.baike.mapper.TbOrderMapper;
import com.baike.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("lessonOrder")
@Api("课程订单接口/和拼团订单共用")
@RefreshScope
public class LessonOrderController {

    @Resource
    private TbOrderMapper orderMapper;

    @ApiOperation("课程订单生成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order",value = "课程订单实体",required = true,paramType = "body")
    })
    @PostMapping
    public Result insertLessonOrder(@RequestBody TbOrder order){
        try {
            order.setOrderId(new IdWorker().nextId());
            order.setCreateTime(new Date());
            order.setStatus(0);
            orderMapper.insert(order);
            return Result.build(ConstantEnum.SUCCESS).setResult("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR).setResult("添加失败");
        }
    }

    //课程订单查询所有接口
    @ApiOperation(value = "课程订单订单查询所有接口")
    @GetMapping
    public Result findOrderAll(){
        List<TbOrder> tbOrders = orderMapper.selectList(null);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbOrders);
    }

    //课程订单id查询接口
    @ApiOperation(value = "课程订单id查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "课程订单id",required = true,paramType = "query")
    })
    @GetMapping("findOrderById")
    public Result findOrderById(@RequestParam("id")Long id){
        TbOrder tbOrder = orderMapper.selectById(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbOrder);
    }

    //用户课程订单查询接口
    @ApiOperation(value = "课程订单用户id查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId",value = "学员id",required = true,paramType = "path")
    })
    @GetMapping("student/{studentId}")
    public Result<TbOrder> findOrderByStudentId(@PathVariable("studentId")Long studentId){
        Map map = new HashMap();
        map.put("student_id",studentId);
        List<TbOrder> list = orderMapper.selectByMap(map);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    //课程订单修改接口
    @ApiOperation(value ="课程订单修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order",value = "课程订单实体类,id必填",required = true,paramType = "body")
    })
    @PutMapping
    public Result updateOrderById(@RequestBody TbOrder order){
        try {
            orderMapper.updateById(order);
            return Result.build(ConstantEnum.SUCCESS).setResult("修改成功~");
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR).setResult("修改失败~");
        }
    }
    //课程订单根据商品订单号修改接口
    @ApiOperation(value ="课程订单根据商品订单号修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outTradeNo",value = "商品订单号",required = true,paramType = "query"),
            @ApiImplicitParam(name = "TradeNo",value = "支付宝流水号",required = true,paramType = "query")
    })
    @PutMapping("updateOrderByOutTradeNo")
    public Result updateOrderByOutTradeNo(@RequestParam("outTradeNo") String outTradeNo,@RequestParam("tradeNo") String TradeNo){
        try {
            TbOrder order = new TbOrder();
            order.setTradeNo(TradeNo);
            order.setStatus(1);
            UpdateWrapper<TbOrder> wrapper = new UpdateWrapper<>();
            wrapper.eq("order_id",outTradeNo);
            orderMapper.update(order,wrapper);
            return Result.build(ConstantEnum.SUCCESS).setResult("修改成功~");
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR).setResult("修改失败~");
        }
    }


    //课程订单超时未支付修改接口
    @ApiOperation(value ="课程订单超时未支付修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outTradeNo",value = "商品订单号",required = true,paramType = "query"),
    })
    @PutMapping("orderTimeOut")
    public Result orderTimeOut(@RequestParam("outTradeNo") String outTradeNo){
        try {
            //先判断订单是否已支付
            Result result = findOrderByOutTradeNo(outTradeNo);
            TbOrder tborder = (TbOrder)result.getResult();
            if ("0".equals(tborder.getStatus().toString())){//0是未支付状态
                TbOrder order = new TbOrder();
                order.setStatus(3);//超时未支付状态
                UpdateWrapper<TbOrder> wrapper = new UpdateWrapper<>();
                wrapper.eq("order_id",outTradeNo);
                orderMapper.update(order,wrapper);
                return Result.build(ConstantEnum.SUCCESS).setResult("修改成功~");
            }
            return Result.build(ConstantEnum.SUCCESS).setResult("交易已完成~");
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR).setResult("修改失败~");
        }
    }

    //根据订单号查询订单
    @ApiOperation(value = "根据订单号查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "课程订单id",required = true,paramType = "path")
    })
    @GetMapping("findOrderByOutTradeNo/{OutTradeNo}")
    public Result findOrderByOutTradeNo(@PathVariable("OutTradeNo")String OutTradeNo){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id",OutTradeNo);
        List list = orderMapper.selectList(wrapper);
        return Result.build(ConstantEnum.SUCCESS).setResult(list.get(0));
    }

    //根据用户id和课程id查询订单
    @ApiOperation(value = "根据用户id和课程id查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId",value = "用户id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "lessonId",value = "课程id",required = true,paramType = "query")
    })
    @GetMapping("findOrder")
    public Result findOrderBystudentId(@RequestParam("studentId")Long studentId,@RequestParam("lessonId")Long lessonId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("student_id",studentId);
        wrapper.eq("lesson_id",lessonId);
        List list = orderMapper.selectList(wrapper);
        if (list.isEmpty()){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(list.get(0));
    }

    //课程订单删除接口
    @ApiOperation(value = "课程订单删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "课程订单id/多选",required = true,paramType = "query")
    })
    @DeleteMapping
    public Result deleteAdOrderById(@RequestParam("ids")Long[] ids){
        try {
            orderMapper.deleteBatchIds(Arrays.asList(ids));
            return Result.build(ConstantEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
    }

}
